/*
 * author: ht
 * desc: 其他杂项
 */

var constant = require('../const');
var models = require('../models');
var AV = require('leanengine')
/*
 *喜欢用户，就是likecount +1
 */
 function likeUser (req,res) {
 	var userId = req.params.userId;
 	var userQuery = new AV.Query(models.MLUser);
 	userQuery.get(userId,{
 		success:function(user){
 			user.increment("likeCount");
 			user.save(null,{
 				success:function(user){
 					res.success('喜欢成功');
 				},
 				error:function(user,error){
 					res.error(error);
 				}
 			})
 		},
 		error: function(error) {
 			res.error('喜欢失败');
 		}
 	})

 }

/*
 *不喜欢用户，就是likecount -1
 */
 function disLikeUser (req,res) {
 	var userId = req.params.userId;
 	var userQuery = new AV.Query(models.MLUser);
 	userQuery.get(userId,{
 		success:function(user){
 			user.increment("likeCount",-1);
 			user.save(null,{
 				success:function(user){
 					res.success('取消喜欢成功');
 				},
 				error:function(user,error){
 					res.error(error);
 				}
 			})
 		},
 		error: function(error) {
 			res.error('取消喜欢失败');
 		}
 	})
 }


/*
 *举报后举报次数+1
 */
 function reportUser (req,res) {
 	var userId = req.params.userId;
 	var userQuery = new AV.Query(models.MLUser);
 	userQuery.get(userId,{
 		success:function(user){
 			var likecount = user.get('reportCount');
 			user.increment("reportCount");
 			user.save(null,{
 				success:function(user){
 					res.success('加一成功');
 				},
 				error:function(user,error){
 					res.error(error);
 				}
 			})
 		},
 		error: function(error) {
 			res.error('加一失败');
 		}
 	})
 }


/*
 *投诉+1
 */
 function complaintUser (req,res) {
 	var userId = req.params.userId;
 	var userQuery = new AV.Query(models.MLUser);
 	userQuery.get(userId,{
 		success:function(user){
 			var likecount = user.get('complaintCount');
 			user.increment("complaintCount");
 			user.save(null,{
 				success:function(user){
 					res.success('加一成功');
 				},
 				error:function(user,error){
 					res.error(error);
 				}
 			})
 		},
 		error: function(error) {
 			res.error('加一失败');
 		}
 	})
 }

//给一个用户创建缩略图
function creatSubAvatar(req,res){
	var userId = req.params.userId;
	var userQuery = new AV.Query(models.MLUser);
	userQuery.get(userId,{
		success:function(user){
			var file = AV.File.withURL('test.jpg', user.get("avatar").url());
			var suburl = file.thumbnailURL(100, 100);
			user.set("subAvatar",suburl)
			user.save(null,{
				success:function(user){
					res.success('成功');
				},
				error:function(user,error){
					res.error(error);
				}
			})
		},
		error: function(error) {
			res.error('加一失败');
		}
	})
}

function getNetTime(req,res){
	res.success((new Date()).getTime());
}

function isHasTestOrder(req,res){
	var userId = req.params.userId;
	var userQuery = new AV.Query(models.MLUser);
	userQuery.get(userId,{
		success:function(user){
			var now = new Date();
  		var dateStart = new Date(now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate());
			var dateEnd = new Date(dateStart.getTime()+86400000);//下一天
			var orderQuery = new AV.Query(models.MLTestOrder);
			orderQuery.equalTo("sponsor",user);
			orderQuery.greaterThan("createdAt",dateStart);
			orderQuery.lessThan("createdAt",dateEnd);
			orderQuery.find({
				success:function(results){
					if(results.length>0){
						res.success("true");
					}else{
						res.success("false");
					}
				},
				error:function(error){
					res.error(error);
				}
			})

		},
		error: function(error) {
			res.error("false");
		}
	})
}

exports.getNetTime = getNetTime;
exports.complaintUser = complaintUser;
exports.isHasTestOrder = isHasTestOrder;
exports.reportUser = reportUser;
exports.likeUser = likeUser;
exports.disLikeUser = disLikeUser;
exports.creatSubAvatar = creatSubAvatar;